<script setup lang="ts" name="Count">


import {ref, toRefs} from "vue";
//引入 useCountStore
import { useCountStore } from '@/store/count'
import {storeToRefs} from "pinia";

//使用得到一个专门保存count的store
let countStore = useCountStore()

const value1 = ref([])
const options = [
  {
    value: '1',
    label: 'Option1',
  },
  {
    value: '2',
    label: 'Option2',
  },
  {
    value: '3',
    label: 'Option3',
  },
  {
    value: '4',
    label: 'Option4',
  },
  {
    value: '5',
    label: 'Option5',
  },
]

//打印一个123



let n = ref(1) //用户选择的数字
const { sum,age,bigSum } = countStore
// console.log(toRefs(countStore), '!!!!!!!!')
console.log(storeToRefs(countStore)) //用这个
// storeToRefs只会关注store中的数据,不会对方法进行toRefs


function add () {
  //在vuex中数据不能直接修改,
  //第一种修改方式
  // countStore.sum += n.value

  //第二种修改方式===>批量变更
  // countStore.$patch( {
  //   sum: 77,
  //   age: 25,
  // })

  //第三种 调用action
  countStore.increment(n.value)
}
function minus () {
  countStore.sum -= 1
}
function changeValue(e:Object) {
  console.log(e)
}
function removeTag(removedValue:string) {
  console.log(removedValue)
}
</script>

<template>
  <div class="count">
    <h2>当前求和为: {{ sum }};放大10倍 {{ bigSum }}</h2>
    <h3>年龄 {{ age }}</h3>
    <select v-model.number = 'n'>
      <option :value="1">1</option>
      <option :value="2">2</option>
      <option :value="3">3</option>
    </select>
    <button @click="add">加</button>
    <button @click="minus">减</button>
    <el-select
        v-model="value1"
        multiple
        placeholder="Select"
        style="width: 240px"
        @change="changeValue($event)"
        @remove-tag="removeTag"
    >
      <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
      />
    </el-select>
  </div>
</template>

<style scoped>
.count {
  background-color: skyblue;
  padding: 10px;
  border-radius: 10px;
  box-shadow: 0 0 10px;
}
select, button {
  margin:  0 5px;
  height: 25px;
}
</style>